const Koa = require('koa');
const config = require('./config'); // 数据库链接配置

// https://www.npmjs.com/package/koa2-cors
const cors = require('koa2-cors');
const koaBody = require('koa-body');

// https://www.npmjs.com/package/koa-bodyparser
const bodyParser = require('koa-bodyparser');

const hotMiddleware = require('koa-webpack-middleware');



const app = new Koa();

/* 不需要链接数据库, 注释 */
// https://github.com/Automattic/mongoose
// const mongoose = require('mongoose');
// mongoose.connect(config.db, {useNewUrlParser:true}, (err) => {
//     if (err) {
//         console.error('Failed to connect to database');
//     } else {
//         console.log('Connecting database successfully');
//     }
// });

// 数据解析
// app.use(cors());
// app.use(bodyParser());
app
  .use(cors())
  .use(bodyParser())
  .use(koaBody({
    multipart: true,
    formidable: {
      maxFileSize: 200 * 1024 * 1024    // 设置上传文件大小最大限制，默认2M
    }
  }))

// 请求路由模块
const user_router = require('./routes/api/user_router');

// app.use(index.routes(), index.allowedMethods())
app.use(user_router.routes()).use(user_router.allowedMethods());

app.listen(config.port);
console.log(`app.listen(${config.port});`)